<extend name="Base/common"/>
<block name="title"><title>登录 - {:C('WEB_SITE_TITLE')}</title></block>

<block name="content">
  <script type="text/javascript" src="__STATIC__/Baidu-Frontia-JS-1.0.0.js"></script>
  <div class="container">
    <div id="freshberry" class="section_title" style="margin-top:30px;">
      <h3 style="color: #7E7E7E;"><i class="fa fa-cube"></i> 合作伙伴账号登录</h3>
    </div>
    <hr style="border-top: 1px solid #B0B0B0;width: 50%;">
    <div class="row" style="margin-top:20px;">
      <ul class="social">
        <li>
          <a href="javascript:;" class="so_btn" id="qqdenglu"><i class="fa fa-qq"></i> QQ</a>
        </li>
        <li>
          <a href="javascript:;" class="so_btn" id="sinaweibo"><i class="fa fa-weibo"></i> 新浪微博</a>
        </li>      
        <li>
          <a href="javascript:;" class="so_btn" id="renren"><i class="fa fa-renren"></i> 人人网</a>
        </li>
        <li>
          <a href="javascript:;" class="so_btn" id="baidu"><i class="fa fa-paw"></i> 百度</a>
        </li>
        <li>
          <a href="javascript:;" class="so_btn" id="qqweibo"><i class="fa fa-tencent-weibo"></i> 腾讯微博</a>
        </li>
      </ul>
      <div class="Validform_checktip" style="text-align:center;color:red;"></div>
    </div>

    <div id="freshberry" class="section_title" style="margin-top:60px;">
      <h3 style="color: #7E7E7E;"><i class="fa fa-cube"></i> 浆果铺账号登录</h3>
    </div>
    <hr style="border-top: 1px solid #B0B0B0;width: 50%;">
    <div class="row" >
      <form class="form-horizontal" action="__SELF__" method="post">
        <div class="form-group">
          <label class="col-sm-5 control-label" for="inputEmail">用户名</label>
          <div class="col-sm-3">
            <input type="text" id="inputEmail" class="form-control" placeholder="请输入用户名"  ajaxurl="/member/checkUserNameUnique.html" errormsg="请填写1-16位用户名" nullmsg="请填写用户名" datatype="*1-16" value="" name="username">
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-5 control-label" for="inputPassword">密码</label>
          <div class="col-sm-3">
            <input type="password" id="inputPassword"  class="form-control" placeholder="请输入密码"  errormsg="密码为6-20位" nullmsg="请填写密码" datatype="*6-20" name="password">
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-5 control-label" for="inputPassword">验证码</label>
          <div class="col-sm-3">
            <input type="text" id="inputPassword" class="form-control" placeholder="请输入验证码"  errormsg="请填写5位验证码" nullmsg="请填写验证码" datatype="*5-5" name="verify">
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-5 control-label"></label>
          <div class="col-sm-3">
              <img class="verifyimg reloadverify" alt="点击切换" src="{:U('verify')}" style="cursor:pointer;">
          </div>
          <div class="controls Validform_checktip text-warning"></div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-5 col-sm-3">
            <div class="checkbox">
              <label>
                <input type="checkbox"> 自动登录
              </label>
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-5 col-sm-3">
            <button type="submit" class="btn btn-primary">登 录</button>
          </div>
        </div>
      </form>
    </div>
  </div>
	<script type="text/javascript">
      // 网站自己的登录
    	$(document)
	    	.ajaxStart(function(){
	    		$("button:submit").addClass("log-in").attr("disabled", true);
	    	})
	    	.ajaxStop(function(){
	    		$("button:submit").removeClass("log-in").attr("disabled", false);
	    	});


    	$("form").submit(function(){
    		var self = $(this);
    		$.post(self.attr("action"), self.serialize(), success, "json");
    		return false;

    		function success(data){
    			if(data.status){
    				window.location.href = data.url;
    			} else {
    				self.find(".Validform_checktip").text(data.info);
    				//刷新验证码
    				$(".reloadverify").click();
    			}
    		}
    	});

		$(function(){
			var verifyimg = $(".verifyimg").attr("src");
            $(".reloadverify").click(function(){
                if( verifyimg.indexOf('?')>0){
                    $(".verifyimg").attr("src", verifyimg+'&random='+Math.random());
                }else{
                    $(".verifyimg").attr("src", verifyimg.replace(/\?.*$/,'')+'?'+Math.random());
                }
            });
		});


    // 社会化登录
    (function (frontia) {

    // API key 从应用信息页面获取
    var AK = 'U0K09cjgCq56AtpKFUo3RQ3A';
    // 在应用管理页面下的 社会化服务 - 基础设置中设置该地址
    var redirect_url = 'http://127.0.0.1/berrypud/index.php?s=/Home/User/login.html';

    // 初始化 frontia
    frontia.init(AK);

    // 设置登录成功后的回调
    frontia.social.setLoginCallback({
      success: function (user) {
        console.log(user);
        socialLogin(user.getName(), user.getId(), user.getMediaType(), user.getAccessToken(), user.getExpiresIn());
      },
      error: function (error) {
        $('.Validform_checktip').html("登录失败："+ error);
        console.log('error');
        console.log(error)
      }
    });

    var media_type;
    // 点击登录按钮
    $('.so_btn').click(function() {
      media_type = $(this).attr('id');
      // 初始化登录的配置
      var options = {
        response_type: 'token',
        media_type: media_type,  // 登录百度帐号
        redirect_uri: redirect_url,
        client_type: 'web',
      };

      // 登录
      frontia.social.login(options);
    });

    var user;
    user = frontia.getCurrentAccount();

    // 判断用户是否登录
    if (user && user.getType() === 'user' && user.getMediaType() === media_type) {
    }
  }(baidu.frontia));


function socialLogin(name, social_id, media_type, access_token, expires_in)  {
  $.post("{:U('/Home/User/socialLogin')}",
    {
      username: name,
      social_id: social_id,
      media_type: media_type,
      access_token: access_token,
      expires_in: expires_in
    },
    function(data){
      if(data.status){
        window.location.href = data.url;
      } else {
        self.find(".Validform_checktip").text(data.info);
    }
  });
}

</script>
</block>